package cn.hx.mall.order.controller;

import cn.hx.mall.goods.service.ProductService;
import cn.hx.mall.order.entity.OrderReturnApply;
import cn.hx.mall.order.entity.PageModel;
import cn.hx.mall.order.service.CompanyAddressService;
import cn.hx.mall.order.service.OrderReturnApplyService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单退货申请(OrderReturnApply)表控制层
 *
 * @author makejava
 * @since 2022-11-02 17:56:46
 */
@Controller
@RequestMapping("order")
public class OrderReturnApplyController {
    /**
     * 服务对象
     */
    @Resource
    OrderReturnApplyService orderReturnApplyService;
    @Resource
    CompanyAddressService companyAddressService;

    @RequestMapping("returnApplyList")
    public String search(OrderReturnApply orderReturnApply, Model model, String page, String size){
        if (page==null){
            page="1";
        }
        if (size == null) {
            size = "8";
        }
        Integer integer = orderReturnApplyService.queryAll1(orderReturnApply);
        orderReturnApply.setOffset((Integer.valueOf(page)-1)*Integer.valueOf(size));
        orderReturnApply.setLimit(Integer.valueOf(size));
        List<OrderReturnApply> all = orderReturnApplyService.queryAll(orderReturnApply);
        PageModel<OrderReturnApply> pageModel = new PageModel<>();
        pageModel.setCount(integer);
        pageModel.setList(all);
        pageModel.setPage(Integer.valueOf(page));
        pageModel.setSize(Integer.valueOf(size));
        pageModel.setPageSize(integer % Integer.valueOf(size) == 0 ? integer / Integer.valueOf(size) : (integer / Integer.valueOf(size) + 1));
        model.addAttribute("pageModel",pageModel);
        model.addAttribute("returnStatus",orderReturnApplyService.selectStatus());
        return "order/returnApplyList";
    }

    @GetMapping("checkOrder")
    public String checkOrder(int id,Model model){
        model.addAttribute("product",orderReturnApplyService.queryById((long) id));
        model.addAttribute("companyAddress",companyAddressService.queryAllByLimit(0,10000));
        return "order/checkOrder";
    }

    @PostMapping("saveOrder")
    public String saveOrder(OrderReturnApply orderReturnApply){
        OrderReturnApply update = orderReturnApplyService.update(orderReturnApply);
        return "redirect:/order/returnApplyList";
    }

}